    
clear;
load('datamarketB')

%zeta=0.02;

eta0=0.02;
eta1=0.1;



lambda0=lambda0G;

for j=1:length(ss)
s=ss(j);
betas=beta0+beta1*s;
lambdas=max(lambda0*(1-zeta0*s^zeta1),0);
lambda(j)=lambdas;

zetas=max(eta0*(1-s^eta1),0);
zetan(j)=zetas;
%rp=gamma*sigma^2+lambdas*(betas/(betas-gamma)-betas/(betas+1-gamma))-lambdas/(betas+1);
computestarG;
ixx(j)=ix;
qx(j)=1/(1-etaI*ix);
xx(j)=x;
bx(j)=(A-ix-x)^(1/(1-psi))*(rho*(1/(1-etaI*ix)))^(-psi/(1-psi));
dbx(j)=(A-ix-x)^(-1/psi)*rho/(1-etaX*x/s)*bx(j)^(1/psi);

phii=ix-etaI*ix^2/2-deltaI;



 gx(j)=phii-lambdas/(betas+1)-zetan(j)*(qx(j)-qxB(j))/qx(j);
 cx(j)=(bx(j)^(1-1/psi)/rho*(1-etaI*ix))^(-psi);

 
  %  r=cx(j)/qx(j)+gx(j)-rp;
  theta=x*(1-etaI*ix);
  qB(j)=(A-ix)/(r+rp+theta-phii+lambdas/(betas+1));
  qG(j)=(A-ix-x)/(r+rp-phii+lambdas/(betas+1));
  qs(j)=1/(1-etaI*ix);
 %rpx(j)=rp;
 rpB(j)=rp+theta;
%rm(j)=r+rp;
% ux(j)=((r+rp-phii+lambdas/(betas+1))*rho^(-psi))^(1/(1-psi));
 %cx(j)=(r+rp-phii+lambda/(betas+1))*qB(j);
 alphastarx(j)=x/(A-ix-cbar);


end
bs=bx;
dbs=dbx;

bs0=bx;
ixx0=ixx;
xx0=xx;

vol=sqrt(sigma^2+2*lambdas/betas^2)


for j=1:length(ss)-1
dbxnu(j)=(bx(j+1)-bx(j))/ds;
end
dbxnu(j+1)=2*dbxnu(j)-dbxnu(j-1);
i=1;
while dbxnu(i)>dbx(i) && i<length(ss)
i=i+1;
end
flagstar=i;

xx(flagstar)
ss(flagstar)

for j=1:length(ss)
 phiI(j)=ixx(j)-etaI/2*ixx(j)^2-deltaI;
 phiX(j)=xx(j)/ss(j)-etaX/2*xx(j)^2/ss(j)^2-deltaX;
end

save('datastaticG')
computedynamicG;
for j=1:length(ss)
 phiI(j)=ixx(j)-etaI/2*ixx(j)^2-deltaI;
 phiX(j)=xx(j)/ss(j)-etaX/2*xx(j)^2/ss(j)^2-deltaX;
end
ssmk2=ss;bsmk2=bs;phiImk2=phiI;phiXmk2=phiX;xmk2=xx;ixmk2=ixx; flagstarmk2=flagstar;

alpha=0.2;
% qx=1./(1-etaI*ixx);
% for j=2:flagstar
% dqx(j)=(qx(j)-qx(j-1))/ds;
% end
computeassetpriceG;
rpx2=rpx; rm2=rm; rx2=rx; rpU2=rpU;rpS2=rpS; mx2=mx; 



save('datamarketG2');
